package com.kyrie.lib_sys.utils.learn.TimeDemo;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Stream;

@Slf4j
public class TimeDemo09 {

    //定义时间格式
    public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    //有一个List<Map<String,String>> timeList的集合,  里面的map存有时间数据prepareTime,travelTime,startTime,completeTime
    public static boolean timeListCheck(List<Map<String, String>> timeList) {

        /**
         * 1: 定义一个Map用来存储人员和工时 按人员分组,每个人员有不同的工时区间 (人员标识: pk_pandoc)
         */
        Map<String, List<Map<String, String>>> EmpTimeMap = new HashMap<>();


        // 2:遍历传进来的timeList集合
        log.info("timeList集合: {} ", timeList);
        for (Map<String, String> timeMap : timeList) {
            String pkPandoc = timeMap.get("pk_pandoc");
            if (StringUtils.isBlank(pkPandoc)) {
                throw new RuntimeException(" pk_pandoc 为空");
            }

            /**
             * 3:定义一个TimePoints 列表集合 存储所有时间点
             */
            List<LocalDateTime> timesPoint = new LinkedList<>();

            //4: 从遍历的timeMap集合中获取相关的数据, 然后判空,放到timePoints集合中
            String prepareTime = timeMap.get("prepareTime");
            if (!StringUtils.isBlank(prepareTime)) {
                timesPoint.add(LocalDateTime.parse(prepareTime, formatter));
            }
            String travelTime = timeMap.get("travelTime");
            if (!StringUtils.isBlank(travelTime)) {
                timesPoint.add(LocalDateTime.parse(travelTime, formatter));
            }
            String startTime = timeMap.get("startTime");
            if (!StringUtils.isBlank(startTime)) {
                timesPoint.add(LocalDateTime.parse(startTime, formatter));
            }
            String completeTime = timeMap.get("completeTime");
            if (!StringUtils.isBlank(completeTime)) {
                timesPoint.add(LocalDateTime.parse(completeTime, formatter));
            }

            //5: timePoints集合 不为空,走下面逻辑     要是为空 直接返回
            if (!CollectionUtils.isEmpty(timesPoint)) {
                //6: 从集合中获取开始区间  (min最小时间为开始时间),(max最大时间为结束时间)
                LocalDateTime start = Collections.min(timesPoint);
                LocalDateTime end = Collections.max(timesPoint);

                log.info("开始时间: {} ------ 结束时间: {}", start, end);
                /**
                 * 7: 定义一个currentIntervalMap,用来存储当前时间区间
                 */
                Map<String, String> currentIntervalMap = new HashMap<>();
                currentIntervalMap.put("start", String.valueOf(start));
                currentIntervalMap.put("end", String.valueOf(end));


                /**
                 * 8: exisingTimeMapList,用来存储当前人员已有的时间区间
                 */
                List<Map<String, String>> exisingTimeMapList = EmpTimeMap.get(pkPandoc);

                //9: 如果当前人员已存在的时间区间为空,则返回一个空列表
                if (exisingTimeMapList==null) {
                    exisingTimeMapList = new ArrayList<>();
                }
                //10: 不为空,则遍历已存在的时间区间,获取已存在时间其中min与max时间
                for (Map<String, String> exisingMapList : exisingTimeMapList) {
                    LocalDateTime exisingStart = LocalDateTime.parse(exisingMapList.get("start"));
                    LocalDateTime exisingEnd = LocalDateTime.parse(exisingMapList.get("end"));

                    //11: 用一开始传进来(当前)的时间区间去和已存在的时间区间进行对比
                    if (!(start.isAfter(exisingEnd) || end.isBefore(exisingStart))) {
                        System.out.printf("员工 [%s] 的时间段 [%s ~ %s] 与已存在时间段 [%s ~ %s] 存在重叠错误\n",
                                pkPandoc,
                                start.format(formatter), end.format(formatter),
                                exisingStart.format(formatter), exisingEnd.format(formatter));
                        return false;
                    }

                }

                // 13: 添加当前区间到员工时间列表
                exisingTimeMapList.add(currentIntervalMap);
                EmpTimeMap.put(pkPandoc, exisingTimeMapList);


            }


        }


        return true;

    }

    public static void main(String[] args) {
        List<Map<String, String>> timeList = new ArrayList<>();

        Map<String, String> time1 = new HashMap<>();
        time1.put("pk_pandoc", "E01");
        time1.put("prepareTime", "2025-07-01 09:00:00");
        time1.put("travelTime", "2025-07-01 09:10:00");
        time1.put("completeTime", "2025-07-01 09:30:00");

        Map<String, String> time2 = new HashMap<>();
        time2.put("pk_pandoc", "E01");
        time2.put("prepareTime", "2025-07-01 09:20:00"); // 与 time1 有重叠

        Map<String, String> time3 = new HashMap<>();
        time3.put("pk_pandoc", "E02");
        time3.put("prepareTime", "2025-07-01 10:00:00");
        time3.put("startTime", "2025-07-01 10:30:00");

        timeList.add(time1);
        timeList.add(time2);
        timeList.add(time3);

        boolean result = timeListCheck(timeList);
        System.out.println("校验结果: " + result);
    }


}
